// pages/gps/teachergps.js
var db = wx.cloud.database()
var QQMapWX = require('../../libs/qqmap-wx-jssdk');
var qqmapsdk;
const util = require('util.js');
// const { isSet } = require('lodash');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    classname:"",
    docid:"",
    openid:"",
    showModalStatus: false,
    showstart:true,
    ani:"",
    metre:500,
    effectivetime:20*60,
    nowtime:"",
    nowplace:"",
    nowlocation:404,
    yqdname:[],
    allname:[],
    wqdname:[],
    wqdlenth:"0",
    yqdlenth:"0"
  },
  abb:function(e){
    console.log("获取用户设置的rtime和range",e.detail.value)
    if(e.detail.value.range!="" && e.detail.value.rtime!=""){
      
      var myDate = new Date();
      myDate.setTime(myDate.getTime()+e.detail.value.rtime*60*1000);
      myDate = util.formatDate(myDate, 'yyyy-mm-dd hh:mi:ss');
      console.log("当前时间为:",myDate);
      
      this.setData({
        metre:e.detail.value.range*1.0,
        effectivetime:e.detail.value.rtime*60,
        nowtime:myDate 
      })    
    }
    

    },
  starcheck:function(){
    wx.showLoading({
      title: '发起签到中',
      mask:true
    })
    this.setData({
      showstart:false
    })
    wx.cloud.callFunction({
      name:"update",
      data:{
        table:"class",
        docid:this.data.docid,
        shux:"cheak_teacher",
        classname:{
            yqdname:[],
            checkif:true,
            cantime:this.data.nowtime,
            location:this.data.nowlocation,
            canmeter:this.data.metre
        }
      }
    }).then(res=>{
      wx.hideLoading()
    })
  },
  stop:function(){
    wx.showLoading({
      title: '暂停签到中',
      mask:true
    })
    this.setData({
      showstart:true
    })
    wx.cloud.callFunction({
      name:"update",
      data:{
        table:"class",
        docid:this.data.docid,
        shux:"cheak_teacher",
        classname:{
            checkif:false,
        }
      }
    }).then(res=>{
      wx.hideLoading()
    })
  },
  xs:function(){
    this.wxonload()
    var animation = wx.createAnimation({
      duration: 300,
      timingFunction: 'linear',
      delay: 10
     });
    animation.rotate(360).step()
    this.setData({
     ani: animation.export()
    })

  
    setTimeout(() => {
      animation.rotate(0).step()
      this.setData({
       ani: animation.export()
      })
     },305)

  },
  gpsqx:function(){
    wx.authorize({
      scope: 'scope.userLocation',//发起定位授权
      success: function () {
        console.log('有定位授权')
      //授权成功，此处调用获取定位函数
      }, fail() {
     //如果用户拒绝授权，则要告诉用户不授权就不能使用，引导用户前往设置页面。
        console.log('没有定位授权')
        wx.showModal({
          cancelColor: 'cancelColor',
          title: '没有授权无法获取位置信息',
          content: '是否前往设置页面手动开启',
          success: function (res) {
            if (res.confirm) {
              wx.openSetting({
                withSubscriptions: true,
              })
            } else {
              wx.showToast({
                icon: 'none',
                title: '您取消了定位授权',
              })
            }
          }, fail: function (e) {
            console.log(e)
          }
        })
      }
    })
    
      this.gpsdw();
   
     
  },
  gpsdw:function(){
    var _that=this;
    wx.getLocation({
      type: 'gcj02',
     }).then(res=>{
      const latitude = res.latitude
      const longitude = res.longitude
      const speed = res.speed
      const accuracy = res.accuracy
      console.log("gps",res.latitude,res.longitude)
      _that.setData({
        nowlocation:{
          longitude:longitude,
          latitude:latitude
        }
      })
      qqmapsdk.reverseGeocoder({
        location:{
          latitude:latitude,
          longitude:longitude
        },
        success: function(res) {//成功后的回调
          console.log(res.result.address);
          _that.setData({
            nowplace:res.result.address,
            
          })
        }
      })
     })
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
 
  onLoad: function (options) {
    var _that=this;
  qqmapsdk = new QQMapWX({
        key: 'QD5BZ-53ZRF-4MHJF-JD6NW-Y4K3F-PPFOH'
      });
  console.log(options)
  var user = wx.getStorageSync("user");
  console.log("user",user)

  db.collection("class").where({  
    classname:options.classname,
    openid:user.openid
  }).get().then(res=>{
    if(res.data.length==1){
    console.log(res.data[0]._id)
    if(res.data[0].checkif == true){
      this.setData({
        showstart:false
      })
    }
    if(res.data[0].students.length!=0){

      this.setData({
        allname:res.data[0].students
      })
      if(res.data[0].yqdname.length!=0){
        this.setData({
          yqdname:res.data[0].yqdname,
          yqdlenth:res.data[0].yqdname.length
        })
        this.data.allname.forEach(function(element,index){
          _that.data.yqdname.forEach(function(element2){
            if(element == element2){
              // console.log("该同学已签到",element)
              _that.data.allname.splice(index,1)
              index=index-1;
            }
          });
        })
        this.setData({
          wqdname:_that.data.allname,
          wqdlenth:_that.data.allname.length
        })
        
      }else{
        this.setData({
          wqdname:_that.data.allname,
          wqdlenth:_that.data.allname.length
        })
      }
    }
   
    }else{
      console.log("数据库获取班级信息出错")
    }
    //获取data格式的时间
    const util = require('util.js');
    var myDate = new Date();
    myDate = util.formatDate(myDate, 'yyyy-mm-dd hh:mi:ss');
    console.log("当前时间为:",myDate);

    
    this.setData({
      classname:options.classname,
      openid:user.openid,
      docid:res.data[0]._id,
      nowtime:myDate
    })
    this.gpsqx()
  })
  
 
  },

  wxonload:function(){
    var _that=this;
    var user = wx.getStorageSync("user");
    // console.log("user",user)
    // console.log("classname 1",this.data)
    db.collection("class").where({  
      classname:_that.data.classname,
      openid:user.openid
    }).get().then(res=>{
      if(res.data.length==1){
      if(res.data[0].checkif == true){
        console.log(res.data[0])
        this.setData({
          showstart:false
        })
      }
      if(res.data[0].students.length!=0){
       
        this.setData({
          allname:res.data[0].students
        })
        if(res.data[0].yqdname.length!=0){
          this.setData({
            yqdname:res.data[0].yqdname,
            yqdlength:res.data[0].yqdname.length
          })
          this.data.allname.forEach(function(element,index){
            _that.data.yqdname.forEach(function(element2){
              if(element == element2){
                // console.log("该同学已签到",element)
                _that.data.allname.splice(index,1)
                index=index-1;
              }
            });
          })
          this.setData({
            wqdname:_that.data.allname,
            wqdlenth:_that.data.allname.length
          })
          
        }else{
          this.setData({
            wqdname:_that.data.allname,
            wqdlenth:_that.data.allname.length,
            yqdname:[],
            yqdlenth:0
          })
        }
      }
     
      }else{
        console.log("数据库获取班级信息出错")
      }
      //获取data格式的时间
      const util = require('util.js');
      var myDate = new Date();
      myDate = util.formatDate(myDate, 'yyyy-mm-dd hh:mi:ss');
      console.log("当前时间为:",myDate);
  
      
      this.setData({
        
        openid:user.openid,
        docid:res.data[0]._id,
        nowtime:myDate
      })
      this.gpsqx()
    })
    
   
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 调用接口
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  powerDrawer: function (e) { 


    var currentStatu = e.currentTarget.dataset.statu; 
    this.util(currentStatu) 
   }, 
   util: function(currentStatu){ 
    /* 动画部分 */
    // 第1步：创建动画实例  
    var animation = wx.createAnimation({ 
     duration: 200, //动画时长 
     timingFunction: "linear", //线性 
     delay: 0 //0则不延迟 
    }); 
      
    // 第2步：这个动画实例赋给当前的动画实例 
    this.animation = animation; 
    
    // 第3步：执行第一组动画 
    animation.opacity(0).rotateX(-100).step(); 
    
    // 第4步：导出动画对象赋给数据对象储存 
    this.setData({ 
     animationData: animation.export() 
    }) 
      
    // 第5步：设置定时器到指定时候后，执行第二组动画 
    setTimeout(function () { 
     // 执行第二组动画 
     animation.opacity(1).rotateX(0).step(); 
     // 给数据对象储存的第一组动画，更替为执行完第二组动画的动画对象 
     this.setData({ 
      animationData: animation 
     }) 
       
     //关闭 
     if (currentStatu == "close") { 
      this.setData( 
       { 
        showModalStatus: false
       } 
      ); 
     } 
    }.bind(this), 200) 
     
    // 显示 
    if (currentStatu == "open") { 
     this.setData( 
      { 
       showModalStatus: true
      } 
     ); 
    } 
   } 
})